﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AODL.Document;
using AODL.Document.TextDocuments;
using AODL.Document.Content.Text;
using AODL.Document.Styles;
using System.Collections;
using System.IO;
using System.Threading;

namespace SteGoFoNt
{
    public partial class Form1 : Form
    {
        OpenFileDialog fileDialog = new OpenFileDialog();

        public Form1()
        {
            InitializeComponent();            
        }

        private void _btnbuscar_Click(object sender, EventArgs e)
        {            
            fileDialog.Filter = "ODT files (*.odt)|*.odt";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                _txtDirectorio.Text = fileDialog.FileName;
            }
        }

        private void _btnocuntar_Click_1(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(_txtDirectorio.Text))
            {
                File.Copy(_txtDirectorio.Text, "Stegano.odt", false);

                string binario = ConvertirABinario(txMensaje.Text);
                int count = binario.Length;
                int index = 0;

                Thread.Sleep(3000);

                TextDocument doc = new TextDocument();
                doc.Load("C:\\Documents and Settings\\Mauricio\\Escritorio\\Stegano.odt");

                bool flag = true;

                for (int p = 0; p < doc.Content.Count; p++)
                {
                    if (!flag)
                        break;

                    Paragraph paragraph = (Paragraph)doc.Content[p];

                    for (int y = 0; y < paragraph.MixedContent.Count; y++)
                    {
                        try
                        {
                            if (binario[index] == '1')
                                ((FormatedText)paragraph.MixedContent[y]).TextStyle.TextProperties.FontColor = "#FFFFFF";
                            index++;
                        }
                        catch (Exception)
                        {
                            flag = false;
                            break;
                        }
                    }
                }

                Thread.Sleep(3000);

                doc.SaveTo("C:\\Documents and Settings\\Mauricio\\Escritorio\\Stegano.odt");
                doc.Dispose();
            }
        }

        private string  ConvertirABinario(string strText)
        {
            byte[] btText;
            btText = System.Text.Encoding.UTF8.GetBytes(strText);
            Array.Reverse(btText);
            BitArray bit = new BitArray(btText);
            StringBuilder sb = new StringBuilder();


            for (int i = bit.Length - 1; i >= 0; i--)
            {
                if (bit[i] == true)
                {
                    sb.Append(1);
                }
                else
                {
                    sb.Append(0);
                }
            }           
            return sb.ToString();
        }


        private void _btnsalir_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
